package com.airlines.crewassignment.controller.crewdispatch;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.base.entity.dict.Select2;
import com.airlines.base.entity.user.User;
import com.airlines.common.ListSortUtil;
import com.airlines.common.Page;
import com.airlines.common.R;
import com.airlines.common.StringToListUtil;
import com.airlines.crewassignment.constant.CrewConstant;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.domain.FlightInfo;
import com.airlines.crewassignment.entity.crewdispatch.AdditionalCrew;
import com.airlines.crewassignment.service.airport.AirportService;
import com.airlines.crewassignment.service.crew.CrewService;
import com.airlines.crewassignment.service.crewdispatch.AdditionalCrewService;
import com.airlines.crewassignment.service.dict.Select2Service;
import com.airlines.crewassignment.service.linkline.FlightInfoService;
import com.airlines.crewassignment.service.linkline.FullCalendarService;

/**
 * @author xianghaini
 * @date 2018/07/11 14:15:38
 * @company xxxx Airlines
 * @description 随机人员
 */
@Controller
@RequestMapping("/additionalCrew")
public class AdditionalCrewController {

	private static final String PREFIX = "/crewdispatch";

	@Autowired
	AdditionalCrewService additionalCrewService;
	@Autowired
	FullCalendarService fullCalendarService;
	@Autowired
	Select2Service select2Service;
	@Autowired
	CrewService crewService;
	@Autowired
	FlightInfoService flightInfoService;
	@Autowired
	AirportService airprotService;

	/**
	 * 进入页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "", method = { RequestMethod.GET })
	// @RequiresPermissions("crewdispatch:additionalCrew:list")
	String toAdditionalCrewPage(Model model, String moduleFlag) {
		model.addAttribute("moduleFlag", moduleFlag);
		return PREFIX + "/additionalCrewList";
	}

	/**
	 * 多条件模糊 查询并且分页显示
	 * 
	 * @param additionalCrew
	 * @param offset
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Page list(AdditionalCrew additionalCrew, String offset, String limit) {
		Page page = new Page();
		page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
		page.setPageSize(Integer.valueOf(limit));
		page = additionalCrewService.list(additionalCrew, page);
		return page;
	}

	/**
	 * 进入新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.GET })
	// @RequiresPermissions("crewdispatch:additionalCrew:add")
	String add(Model model, String moduleFlag) {
		model.addAttribute("moduleFlag", moduleFlag);
		return PREFIX + "/additionalCrewAdd";
	}

	/**
	 * 保存
	 * 
	 * @param additionalCrew
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = { RequestMethod.POST })
	// @RequiresPermissions("crewdispatch:additionalCrew:add")
	public R save(AdditionalCrew additionalCrew, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("currentUser");
		additionalCrew.setUpdateOp(user.getWorkcode());
		intInfo(additionalCrew);
		if (additionalCrewService.insert(additionalCrew) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 进入编辑页面
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/edit", method = { RequestMethod.GET })
	// @RequiresPermissions("crewdispatch:additionalCrew:edit")
	String edit(Model model, Integer id) {
		AdditionalCrew additionalCrew = additionalCrewService.get(id);
		model.addAttribute("additionalCrew", additionalCrew);
		return PREFIX + "/additionalCrewEdit";
	}

	/**
	 * 修改
	 * 
	 * @param additionalCrew
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	// @RequiresPermissions("crewdispatch:additionalCrew:edit")
	public R update(AdditionalCrew additionalCrew, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("currentUser");
		additionalCrew.setUpdateOp(user.getWorkcode());
		intInfo(additionalCrew);
		if (additionalCrewService.update(additionalCrew) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 进入查看页面
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/view", method = { RequestMethod.GET })
	// @RequiresPermissions("crewdispatch:additionalCrew:list")
	String view(Model model, Integer id) {
		AdditionalCrew additionalCrew = additionalCrewService.get(id);
		model.addAttribute("additionalCrew", additionalCrew);
		model.addAttribute("isReadonly", 1);
		return PREFIX + "/additionalCrewEdit";
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	// @RequiresPermissions("crewdispatch:additionalCrew:delete")
	public R delete(Integer id) {
		if (additionalCrewService.delete(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@ResponseBody
	@RequestMapping(value = "/getflightInfo", method = { RequestMethod.POST })
	// @RequiresPermissions("crewdispatch:additionalCrew:delete")
	public List<FlightInfo> getflightInfo(Date flightDate, Integer crewLinkLineNo) {
		List<FlightInfo> flightInfList = fullCalendarService.listFlight(CrewConstant.DUTY_F, flightDate,
				crewLinkLineNo);
		ListSortUtil.sort(flightInfList, true, "std");
		List<Select2> airportList = select2Service.getAirportCityCnName();
		Map<String, List<Select2>> map = new HashMap<>();
		map.put("departureAirport", airportList);
		map.put("arrivalAirport", airportList);

		List<FlightInfo> result = select2Service.convert(flightInfList, map);
		return result;
	}

	/**
	 * 初始化该随机数据的人员和航班信息
	 * 
	 * @param additionalCrew
	 * @return
	 */
	private AdditionalCrew intInfo(AdditionalCrew additionalCrew) {
		// 人员信息
		additionalCrew.setRandomType("S");
		List<Crew> staff = crewService.staffInfo(additionalCrew.getWorkcode());
		additionalCrew.setPCode(staff.get(0).getPCode());
		additionalCrew.setModuleFlag(staff.get(0).getDutyCode());

		// 航班信息
		List<String> flightIds = StringToListUtil.getList(additionalCrew.getFlightIdList(), ",");
		List<FlightInfo> flightInfos = flightInfoService.getFlightInfoList(flightIds);
		additionalCrew.setSta(
				DateFormatUtils.format((flightInfos.get(flightInfos.size() - 1).getSta()), "yyyy-MM-dd HH:mm:ss"));
		additionalCrew.setStd(DateFormatUtils.format((flightInfos.get(0).getStd()), "yyyy-MM-dd HH:mm:ss"));
		additionalCrew.setArrivalAirport(flightInfos.get(flightInfos.size() - 1).getArrivalAirport());
		additionalCrew.setDepartureAirport(flightInfos.get(0).getDepartureAirport());
		// 连线日期
		additionalCrew.setFlightNo(flightInfoService.getFlightCom(flightInfos));
		additionalCrew.setFlightVoyage(airprotService.getShortCityLine(flightInfos, null));

		return additionalCrew;
	}

}
